﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.SECURITY.BLL;
using System.Data;

namespace KPIS.GERP.SECURITY.App_UI.Forms
{
    public partial class Login : System.Web.UI.Page
    {
        private RoleBLL roleBLL = new RoleBLL();
        private PermissionBLL perBLL = new PermissionBLL();
        private UserBLL userBLL = new UserBLL();
        private UserRoleBLL urBLL = new UserRoleBLL();
        private SsMng ssMng = new SsMng();

        protected void Page_Load(object sender, EventArgs e)
        {
            txtUserID.Focus();
        }

        protected void cmdLogin_Click(object sender, EventArgs e)
        {
            string name = txtUserID.Text;//.Length > 0 ? txtUserID.Text : "3700500776928";
            string password = txtPassword.Text;//.Length > 0 ? txtPassword.Text : "1959";

            if (userBLL.CheckPassword(name, password))
            {
                DataRowCollection users = userBLL.LoadUser(null, null, null, name).Tables[0].Rows;
                if (users != null && users.Count > 0)
                {
                    DataRow loginUser = users[0];

                    Session["EmpSeq"] = loginUser["emp_seq"].ToString();
                    Session["emp_seq"] = loginUser["emp_seq"].ToString();
                    Session["UserSeq"] = loginUser["user_seq"].ToString();
                    Session["UserCode"] = loginUser["user_code"].ToString();

                    ssMng.SetLoginSession(loginUser["user_seq"].ToString(),
                                          loginUser["user_seq"].ToString(),
                                          loginUser["user_code"].ToString());

                    Page.Response.Redirect("Default.aspx");
                }
            }

            RadManager1.Alert("รอการตรวจสอบจากผู้ดูแลระบบ");
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login_Register_Form.aspx");
        }

        protected void btnForget_Click(object sender, EventArgs e)
        {
            Response.Redirect("Forget_Password.aspx");
        }

    }
}
